package Logic;
import java.util.StringTokenizer;
import java.util.Vector;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import System.SystemLogger;

public class Movie {
	private static int globalID = 0;
	private int id;
	private String name; 
	private String director;
	private int length;
	private String language;
	private int year;
	private String country;
	private String primary;
	private String secondary;
	private String[] players;
	private String summary;
	private String mpaa;
	private int rate;//1-10
	final private String[] fields = {"Name","Director","Runtime","Language",
									"Year","Country","pGenre","sGenre","Cast",
									"Plot","MPAA","Rate"};
	private Vector<Rank> ranks = new Vector<Rank>(1);
	private double averageRank = 0;
	
	
	public Movie(String[] vals) throws Exception{		
		try{
			setValues(vals);
			//System.out.println("movie created");
		}
		catch(Exception ex){
			SystemLogger.getInstance().send("Exception: " + ex.getMessage());
			throw ex;
		}		
	}
	
	public Movie(Node node) throws Exception{
		int attNum = fields.length;
		NamedNodeMap attributes;
		String[] vals = new String[attNum];
		try{			
			attributes = node.getAttributes();
			for (int j=0; j<attNum; j++)
				vals[j] = attributes.getNamedItem(fields[j]).getNodeValue();
			
			setValues(vals);
			//System.out.println("movie created");
			
		}
		catch(Exception ex){
			SystemLogger.getInstance().send("Exception: " + ex.getMessage());
			throw ex;
		}		
	}
	
	public void setValues(String[] vals) throws Exception{		
		if (vals.length != fields.length)
			throw new Exception("Movie : number of values incorrect");
		id = globalID;
		globalID++;
		name = vals[0]; 
		director = vals[1];
		length = Integer.valueOf(vals[2]);
		language = vals[3];
		year = Integer.valueOf(vals[4]);
		country = vals[5];
		primary = vals[6];
		if (vals[7]==null)
			secondary = "";
		else
			secondary = vals[7];
		
		StringTokenizer st = new StringTokenizer(vals[8],",");
		int size = st.countTokens();
		this.players = new String[size];
		for (int i=0; i<size; i++)
			this.players[i] = st.nextToken();
		
		summary = vals[9];
		mpaa = vals[10];
		rate = Integer.valueOf(vals[11]);
	}
	
	public String toString(){						
		return name;
	}
	
	public boolean equals(Movie m){
		return (m.getID() == id); 
	}
	
	public void addRank(Rank rank){
		int ranksNum = ranks.size();
		int i = 0;
		while (i<ranksNum){
			if (rank.getUser().getID() <= ranks.get(i).getUser().getID())
				break;
			i++;
		}
		ranks.add(i,rank);		
		double ranksMul = averageRank * ranksNum;
		ranksMul += rank.getRank();
		averageRank = ranksMul / (ranksNum + 1);		
	}
	
	public void printRanks(){
		int size = ranks.size();
		String user;
		int rank;
		System.out.println(name + " : " + averageRank);
		for (int i=0; i<size; i++){
			user = ranks.get(i).getUser().toString();
			rank = ranks.get(i).getRank(); 
			System.out.println(user + " ranked " + rank);
		}
		System.out.println("----------------------------------------");
	}
	
	public int getID(){
		return id;
	}


	// getters and setters
	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public String getDirector() {
		return director;
	}


	public void setDirector(String director) {
		this.director = director;
	}


	public int getLength() {
		return length;
	}


	public void setLength(int length) {
		this.length = length;
	}


	public String getLanguage() {
		return language;
	}


	public void setLanguage(String language) {
		this.language = language;
	}


	public int getYear() {
		return year;
	}


	public void setYear(int year) {
		this.year = year;
	}


	public String getCountry() {
		return country;
	}


	public void setCountry(String country) {
		this.country = country;
	}


	public String getPrimary() {
		return primary;
	}


	public void setPrimary(String primary) {
		this.primary = primary;
	}


	public String getSecondary() {
		return secondary;
	}


	public void setSecondary(String secondary) {
		this.secondary = secondary;
	}


	public String[] getPlayers() {
		return players;
	}


	public void setPlayers(String[] players) {
		this.players = players;
	}

	public String[] getFields(){
		return this.fields;
	}

	public String getSummary() {
		return summary;
	}


	public void setSummary(String summary) {
		this.summary = summary;
	}


	public String getMpaa() {
		return mpaa;
	}


	public void setMpaa(String mpaa) {
		this.mpaa = mpaa;
	}


	public int getRate() {
		return rate;
	}


	public void setRate(int rate) {
		this.rate = rate;
	}

	public Vector<Rank> getRanks() {
		return ranks;
	}

	public void setRanks(Vector<Rank> ranks) {
		this.ranks = ranks;
	}
	
	public String getPlayersString(){
		String ans = "";
		for (int i=0;i<this.players.length-1;i++){
			ans = ans + players[i] + ",";
		}
		ans = ans + players[players.length-1];
		return ans;
	}
	
	public String[] getVals(){
		String [] ans = {this.name,this.director,((Integer)this.length).toString(),this.language,
							((Integer)this.year).toString(),this.country,this.primary,this.secondary,
							this.getPlayersString(),this.summary,this.mpaa,((Integer)this.rate).toString()};
		return ans;									
	}
	

}
